// Copyright (c) Pomelo Foundation. All rights reserved.
// Licensed under the MIT. See LICENSE in the project root for license information.

using System.Data.Common;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.Logging;
using Pomelo.Data.MyCat;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.Storage.Internal
{
    public class MyCatRelationalConnection : RelationalConnection
    {
        public MyCatRelationalConnection(
            [NotNull] RelationalConnectionDependencies dependencies)
            : base(dependencies)
        {
        }

      

        // TODO: Consider using DbProviderFactory to create connection instance
        // Issue #774
        protected override DbConnection CreateDbConnection() => new MyCatConnection(ConnectionString);

        public MyCatRelationalConnection CreateNodeConnection(MyCatDatabaseHost node)
        {
            var csb = new MyCatConnectionStringBuilder(ConnectionString)
            {
                Database = node.Database,
                UserID = node.Username,
                Server = node.Host,
                Port = node.Port,
                Password = node.Password,
                Pooling = false
            };

            var optionsBuilder = new DbContextOptionsBuilder();
            optionsBuilder.UseMyCat(csb.GetConnectionString(true));
            return new MyCatRelationalConnection(Dependencies.With(optionsBuilder.Options));
        }

        public MyCatRelationalConnection CreateMasterConnection(MyCatDatabaseHost node)
        {
            var csb = new MyCatConnectionStringBuilder(ConnectionString) {
                Database = "mysql",
                UserID = node.Username,
                Server = node.Host,
                Port = node.Port,
                Password = node.Password,
                Pooling = false
            };
            var optionsBuilder = new DbContextOptionsBuilder();
            optionsBuilder.UseMyCat(csb.GetConnectionString(true));
            return new MyCatRelationalConnection(Dependencies.With(optionsBuilder.Options));
        }

        public MyCatRelationalConnection CreateMasterConnection()
        {
            var csb = new MyCatConnectionStringBuilder(ConnectionString)
            {
                Database = "mysql",
                Pooling = false
            };

            var optionsBuilder = new DbContextOptionsBuilder();
            optionsBuilder.UseMyCat(csb.GetConnectionString(true));
            return new MyCatRelationalConnection(Dependencies.With(optionsBuilder.Options));
        }
    }
}
